package com.jiuair.cloud.data.revenue.utils;



import cn.hutool.core.codec.Base64;

import org.apache.commons.codec.binary.Hex;
import org.bouncycastle.jce.provider.BouncyCastleProvider;


import javax.crypto.Cipher;

import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.security.Security;



import static cn.hutool.crypto.SecureUtil.generateKey;


public class TripleDeaHelper {



    /**
     * 偏移变量，固定占8位字节
     */
    private final static String IV_PARAMETER = "12345678";
    /**
     * 密钥算法
     */
    private static final String ALGORITHM = "DES";
    /**
     * 加密/解密算法-工作模式-填充模式
     */
    private static final String CIPHER_ALGORITHM = "DES/CBC/PKCS5Padding";
    /**
     * 默认编码
     */
    private static final String CHARSET = "utf-8";

    //加密
    public static String encrypt(String data, String key) {

        try {
            Security.addProvider(new BouncyCastleProvider());
            byte[] contentBytes = data.getBytes("gb2312");
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("desede");
            SecretKey key2 = keyFactory.generateSecret(new DESedeKeySpec(key.getBytes("gb2312")));
            //获取加密对象                       算法   模式 填充
            Cipher cipher = Cipher.getInstance("desede/ECB/PKCS7Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key2);
            //执行加密
            byte[] result = cipher.doFinal(contentBytes);
            return  Hex.encodeHexString(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * DES解密
     *
     * @param data
     * @param key
     * @return
     */
    public static String decrypt(String data, String key) {

        try {
          // byte[] contentBytes = Base64.decode(data);

           byte[]contentBytes = Hex.decodeHex(data.toCharArray());
           Security.addProvider(new BouncyCastleProvider());
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("des");
           SecretKey key2 = keyFactory.generateSecret(new DESKeySpec(key.getBytes("gb2312")));
            //获取加密对象                       算法   模式 填充
            Cipher cipher = Cipher.getInstance("des/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, key2);
            //执行解密
            byte[] result = cipher.doFinal(contentBytes);
            String decStr = new String(result,"gb2312");
            return decStr;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }



    /**
     * 生成密钥
     * @param initKey 初始配置key
     * @param timeStamp Unix时间戳(毫秒
     * @return
     */
    public static String getKey(String initKey, long timeStamp) {
        String key = Md5Helper.md5(timeStamp + initKey);
        return key.substring(5, 29);
    }


    public static String   getDesKey(String reqSign,String key ,String timestamp){
        StringBuilder desKey = new StringBuilder();
        for (int i =0;i<4;i++){
            char[] ar = timestamp.toCharArray();
            char[] re=  reqSign.toCharArray();
            char[] ke = key.toCharArray();
            char a = ar[ar.length-1-i];
            if (Character.isDigit(a)){  // 判断是否是数字
                int num = Integer.parseInt(String.valueOf(a));
                desKey.append(re[num]);
                desKey.append(ke[num]);
            }
        }
        return desKey.toString();
    }

    public static String desResult(String ctripData, String deskey) {

        try {
            // byte[] contentBytes = Base64.decode(data);
            byte[]contentBytes = Hex.decodeHex(ctripData.toCharArray());
           Security.addProvider(new BouncyCastleProvider());
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("des");
            SecretKey key2 = keyFactory.generateSecret(new DESKeySpec(deskey.getBytes("gb2312")));
            IvParameterSpec iv = new IvParameterSpec(deskey.getBytes("gb2312"));
            //获取加密对象                       算法   模式 填充
            Cipher cipher = Cipher.getInstance("des/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, key2,iv);
            //执行解密
            byte[] result = cipher.doFinal(contentBytes);
            String decStr = new String(result,"gb2312");
            return decStr;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
